/*
 * @Author: LGX
 * @Date: 2022/5/1
 * @Description:
 */
import BaseController from '@/lib/base-controller'
import { GET, POST, RequestMapping } from '@/lib/add-router'

@RequestMapping('/api/diary')
export default class DiaryController extends BaseController {
  @POST('/addDiary')
  async addDiary() {
    try {
      const { ctx, service } = this
      const diary = await service.diary.addDiary(ctx.request.body)
      if (!diary) {
        this.fail('新增失败')
        return
      }

      this.success({
        code: 200,
        msg: '新增成功',
      })
    } catch (e) {
      this.fail(e, 500)
    }
  }

  @POST('/deleteDiary')
  async deleteDiary() {
    try {
      const { ctx, service } = this
      const { id } = ctx.request.body
      const diary = await service.diary.deleteDiary(id)
      if (!diary) {
        this.fail('删除失败')
        return
      }
      this.success(diary)
    } catch (e) {
      this.fail(e, 500)
    }
  }

  @POST('/updateDiary')
  async updateDiary() {
    try {
      const { ctx, service } = this
      const { id } = ctx.request.body
      const diary = await service.diary.updateDiary(ctx.request.body, id)
      if (!diary) {
        this.fail('修改失败')
        return
      }
      this.success(diary)
    } catch (e) {
      this.fail(e, 500)
    }
  }

  @GET('/diaryList')
  async userList() {
    try {
      const { ctx, service } = this
      ctx.validate(
        {
          page: { type: 'int' },
          pageSize: { type: 'int' },
        },
        ctx.query
      )
      const param = await service.diary.getDiary(ctx.query)
      this.success(param)
    } catch (e) {
      this.fail(e, 500)
    }
  }

  @GET('/findDiary')
  async findDiary() {
    try {
      const { ctx, service } = this
      const { id } = ctx.request.body
      const diary = await service.diary.findDiary(id)
      this.success(diary)
    } catch (e) {
      this.fail(e, 500)
    }
  }
}
